<?php

/**
 * This is the model class for table "{{currency}}".
 *
 * The followings are the available columns in table '{{currency}}':
 * @property integer $id
 * @property string $codeFrom
 * @property string $codeTo
 * @property double $conversionRate
 * @property integer $updatedTime
 */
class Currency extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Currency the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{currency}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('codeFrom, codeTo, conversionRate', 'required'),
            array('updatedTime', 'numerical', 'integerOnly' => true),
            array('conversionRate', 'numerical'),
            array('codeFrom, codeTo', 'length', 'max' => 10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, codeFrom, codeTo, conversionRate, updatedTime', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'codeFrom' => 'Code From',
            'codeTo' => 'Code To',
            'conversionRate' => 'Conversion Rate',
            'updatedTime' => 'Updated Time',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('codeFrom', $this->codeFrom, true);
        $criteria->compare('codeTo', $this->codeTo, true);
        $criteria->compare('conversionRate', $this->conversionRate);
        $criteria->compare('updatedTime', $this->updatedTime);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function updateAllCurrency() {
        $timechange = 1 * 60 * 60; //1hour
        $currency = Currency::model()->findAll();
        foreach ($currency as $cu) {
            $from = $cu->codeFrom;
            $to = $cu->codeTo;
            $timedb = $cu->updatedTime;
            if ((time() - intval($timedb)) >= $timechange) {
                $cc = new ECurrencyHelper();
                $rate = $cc->convert($from, $to, 1, ECurrencyHelper::USE_YAHOO);
                $cur = Currency::model()->findByAttributes(array('codeFrom' => $from, 'codeTo' => $to));
                $cur->conversionRate = $rate;
                $cur->updatedTime = time();
                $cur->save();
            }
        }
    }

    public static function checkCurrency($currencyCode) {
        $listCurrency = Currency::model()->findAll();
        $found = 0;
        foreach ($listCurrency as $lc) {
            if (strtoupper($currencyCode) === $lc['codeTo']) {
                $found = 1;
                break;
            }
        }
        if ($found) {
            return $currencyCode;
        } else {
            return "USD";
        }
    }

    public static function convert($from, $to, $money) {
        $currency = Currency::model()->findByAttributes(array('codeFrom' => $from, 'codeTo' => $to));
        if (isset($currency)) {
            return $currency->conversionRate * $money;
        } else {
            $cc = new ECurrencyHelper();
            return $cc->convert($from, $to, $money, ECurrencyHelper::USE_YAHOO); //,ECurrencyHelper::USE_GOOGLE
        }
    }

}
